package com.xbh.common.utils;

import com.xbh.common.web.constants.CodeType;
import com.xbh.common.web.core.Result;
import jodd.util.StringUtil;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 效验工具类
 */
public class EffectivenessUtil {
    /**
     * 效验手机号 邮箱
     */
    public static Result phoneAndEmail(String phone, String email) {
        if (!StringUtil.isEmpty(phone)) {
            String regex = "^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(17[013678])|(18[0,5-9]))\\d{8}$";
            if (phone.length() != 11) {
                return Result.failed(CodeType.PARAMETER_ERROR, "手机号不满11位");
            } else {
                Pattern p = Pattern.compile(regex);
                Matcher m = p.matcher(phone);
                boolean isMatch = m.matches();
                if (!isMatch) {
                    return Result.failed(CodeType.PARAMETER_ERROR, "手机号格式错误");
                }
            }
        }
        if (!StringUtil.isEmpty(email)) {
            String regex = "^\\s*\\w+(?:\\.{0,1}[\\w-]+)*@[a-zA-Z0-9]+(?:[-.][a-zA-Z0-9]+)*\\.[a-zA-Z]+\\s*$";
            Pattern p = Pattern.compile(regex);
            Matcher m = p.matcher(email);
            boolean isMatch = m.matches();
            if (!isMatch) {
                return Result.failed(CodeType.PARAMETER_ERROR, "邮箱错误");
            }
        }
        return Result.failed(CodeType.SUCCESS, "验证通过");
    }
    /**
     * 校验密码  密码为 大写字母 小写字母 数字  特殊字符 任选三种组合而成
     */
    public static boolean password(String password){
        String str = "^(?![a-zA-Z]+$)(?![A-Z0-9]+$)(?![A-Z\\W_!@#$%^&*`~()-+=]+$)(?![a-z0-9]+$)(?![a-z\\W_!@#$%^&*`~()-+=]+$)(?![0-9\\W_!@#$%^&*`~()-+=]+$)[a-zA-Z0-9\\W_!@#$%^&*`~()-+=]{8,16}$";
        Pattern p = Pattern.compile(str);
        Matcher m = p.matcher(password);
        return m.matches();
    }

}
